package org.zky.modules.account.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.zky.aop.log.Log;
import org.zky.modules.account.domain.ZkyRoleTab;
import org.zky.modules.account.service.ZkyRoleTabService;
import org.zky.modules.account.service.dto.ZkyRoleTabQueryCriteria;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author chenglong
* @date 2020-03-23
*/
@Api(tags = "role管理")
@RestController
@RequestMapping("/api/zkyRoleTab")
public class ZkyRoleTabController {

    private final ZkyRoleTabService zkyRoleTabService;

    public ZkyRoleTabController(ZkyRoleTabService zkyRoleTabService) {
        this.zkyRoleTabService = zkyRoleTabService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('zkyRoleTab:list')")
    public void download(HttpServletResponse response, ZkyRoleTabQueryCriteria criteria) throws IOException {
        zkyRoleTabService.download(zkyRoleTabService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询role")
    @ApiOperation("查询role")
    @PreAuthorize("@el.check('zkyRoleTab:list')")
    public ResponseEntity<Object> getZkyRoleTabs(ZkyRoleTabQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(zkyRoleTabService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增role")
    @ApiOperation("新增role")
    @PreAuthorize("@el.check('zkyRoleTab:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ZkyRoleTab resources){
        return new ResponseEntity<>(zkyRoleTabService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改role")
    @ApiOperation("修改role")
    @PreAuthorize("@el.check('zkyRoleTab:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ZkyRoleTab resources){
        zkyRoleTabService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除role")
    @ApiOperation("删除role")
    @PreAuthorize("@el.check('zkyRoleTab:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        zkyRoleTabService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}